﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools.Ribbon;
using RedmineToolBar.Redmine.Configuration;
using RedmineToolBar.Utility;
using System.Windows.Forms;
namespace RedmineToolBar
{
    public partial class frmConfigRedmine : Form
    {
        public frmConfigRedmine()
        {
            InitializeComponent();
        }

        private void btnSalva_Click(object sender, EventArgs e)
        {

            try
            {
                var confBuilder = new RedmineConfigurationBuilder();
                confBuilder
                    .SetURL(txtUrl.Text)
                    .SetUser(txtUser.Text)
                    .SetPwd(txtPwd.Text).Save();
                this.Hide();

                RibbonUtility.SetEnable(RibbonStatus.CONFIGURED);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }


        private void frmConfigRedmine_Load(object sender, EventArgs e)
        {

            
            var confBuilder = new RedmineConfigurationBuilder();
            var config = confBuilder.GetRedmineCongfiguration();
            if (!confBuilder.isWorkbookConnected())
            {
                RibbonUtility.SetEnable(RibbonStatus.NOT_CONFIGURED);

            }
            else if (!confBuilder.isQuerySelected())
            {
                RibbonUtility.SetEnable(RibbonStatus.CONFIGURED);

            }
            else {
                RibbonUtility.SetEnable(RibbonStatus.CONNECTED_TO_QUERY);
            }

            txtPwd.Text = config.Pwd;
            txtUser.Text = config.User;
            txtUrl.Text = config.Url;

        }

    }
}
